package com.mh.web.controller.community;

import com.mh.common.core.controller.BaseController;
import com.mh.common.core.domain.BaseResp;
import com.mh.common.core.page.PageResult;
import com.mh.community.domain.HjyCommunity;
import com.mh.community.domain.dto.HjyCommunityDTO;
import com.mh.community.domain.vo.HjyCommunityVO;
import com.mh.community.service.HjyCommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/community")
@Slf4j
public class HjyCommunityController extends BaseController {

    @Autowired
    private HjyCommunityService cs;

    @GetMapping("/list")
    @PreAuthorize("@MyPermsCheck.checkPerm('system:community:list')")
    public PageResult list(HjyCommunity comm) {

        //获取分页参数-->省略
        //开启PageHelper分页插件-->调用父类方法,省略super.
        startPage();

        //调用Service层查询数据库, 传入实体类对象-->正常查询
        List<HjyCommunityDTO> list = cs.selectList(comm);

        //封装数据-->省略
        //调用父类方法,分页数据封装返回,省略super.
        return getData(list); //返回结果类型PageResult
    }

    //新增小区(插入数据)
    @PostMapping //前端post请求进入此控制单元
    public BaseResp add(@RequestBody HjyCommunity comm) {

        //BaseResp封装响应的数据-->code,msg,<T>data-->code
        //msg在BaseController的toAjax()方法中已设置, 在此调用toAjax(),传入受影响行数即可
        //如果向前端响应, BaseController的toAjax()方法处理
        return  toAjax(cs.insertCom(comm));
    }

    //查询小区信息(根据ID)
    @GetMapping("/{communityId}") //接口文档中定义前端会传入communityId参数
    public BaseResp getInfo(@PathVariable("communityId") Long communityId) {
        //cs.selectComById返回 HjyCommunity对象-->再传入给BaseResp.success-->响应前端JSON
        return BaseResp.success(cs.selectComById(communityId));
    }

    //修改小区信息(根据ID)
    @PutMapping
    public BaseResp edit(@RequestBody HjyCommunity comm) {
        return toAjax(cs.updateCom(comm));
    }

    //删除小区信息(根据IDs)
    @DeleteMapping("/{communityIds}")
    public BaseResp delete(@PathVariable Long[] communityIds){
        return toAjax(cs.deleteComById(communityIds));
    }

    @GetMapping("/queryPullDown") //根据接口文档
    public BaseResp queryPullDown(HjyCommunity comm) {
        //打印入参日志(前提: 必须类前有@Slf4j注解)
        log.info("comm参数为: {}" , comm);
        //输出内容
        //comm参数为: HjyCommunity{communityId=null, communityName='null', communityCode='null', communityProvinceCode='null', communityCityCode='null', communityTownCode='null', communityDetailedAddress='null', communityLongitude='null', communityLatitude='null', deptId=null, communitySort=null}

        List<HjyCommunityVO> vos = null;
        try {
            vos = cs.selectPullDown(comm);
        } catch (Exception e) {
            //throw new RuntimeException(e); -->不用这样的
            log.warn("获取小区下拉列表信息失败",e);
        }

        log.info("响应前端的数据为: {}" , vos);
        //输出内容
        //响应前端的数据为: [HjyCommunityVO{communityId=1623660256618201090, communityName='宏福苑小区'}, HjyCommunityVO{communityId=1623660256618201099, communityName='中海寰宇天悦'}, HjyCommunityVO{communityId=1896829159706689111, communityName='向海明珠小区'}, HjyCommunityVO{communityId=1896829159706689538, communityName='首付未来城小区'}]

        return BaseResp.success(vos);
    }
}
